package main;

import connectDB.ConnectDB;
import DAO.Login;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.PlainDocument;

public class frm_STAFF_A01 extends javax.swing.JFrame {

    Login login = new Login();
    private ConnectDB connect = new ConnectDB();
    private String EmpCode;

    public frm_STAFF_A01() {
        initComponents();
        setLocationRelativeTo(null);
        setResizable(false);
        // Giới hạn ký tự nhập vào field EmpCode là 4 và EmpPass là 20
        // Dùng phương thức getPlainDocument(int n)
        tfEmpCode.requestFocus();
        tfEmpCode.setDocument(getPlainDocument(4));
        tfPass.setDocument(getPlainDocument(20));

        lbEmpCodeTest.setVisible(false);
        lbPasswordTest.setVisible(false);
        lbShowMessage.setVisible(false);


        // CODE TEST
        tfEmpCode.setText("E1");
        tfPass.setText("1234567");


    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfEmpCode = new javax.swing.JTextField();
        tfPass = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        lbEmpCodeTest = new javax.swing.JLabel();
        lbPasswordTest = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lbShowMessage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Employee Code");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Password");

        tfEmpCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfEmpCodeActionPerformed(evt);
            }
        });
        tfEmpCode.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfEmpCodeFocusLost(evt);
            }
        });
        tfEmpCode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfEmpCodeKeyPressed(evt);
            }
        });

        tfPass.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfPassFocusLost(evt);
            }
        });
        tfPass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPassKeyPressed(evt);
            }
        });

        btnLogin.setText("Log In");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        btnLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnLoginKeyPressed(evt);
            }
        });

        lbEmpCodeTest.setBackground(new java.awt.Color(255, 255, 255));
        lbEmpCodeTest.setForeground(new java.awt.Color(255, 0, 0));
        lbEmpCodeTest.setText("*");

        lbPasswordTest.setForeground(new java.awt.Color(255, 0, 0));
        lbPasswordTest.setText("*");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnLogin))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(tfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbPasswordTest))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(tfEmpCode, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbEmpCodeTest)))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tfEmpCode, tfPass});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEmpCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(lbEmpCodeTest))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbPasswordTest))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnLogin)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        jLabel1.setText("LOG IN");

        lbShowMessage.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lbShowMessage.setForeground(new java.awt.Color(255, 0, 0));
        lbShowMessage.setText("Employee Code or Password is not valid");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbShowMessage)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(124, 124, 124)
                            .addComponent(jLabel1))
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(64, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(lbShowMessage)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        loginAction();
    }//GEN-LAST:event_btnLoginActionPerformed

    // Giới hạn ký tự nhập vào textField
    public static PlainDocument getPlainDocument(final int maxLength) {
        PlainDocument p = new PlainDocument() {
            @Override
            public void insertString(int arg0, String arg1, AttributeSet arg2) {
                // TODO Auto-generated method stub
                if (getLength() + arg1.length() <= maxLength) {
                    try {
                        super.insertString(arg0, arg1, arg2);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        };
        return p;
    }

    private void tfEmpCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfEmpCodeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfEmpCodeActionPerformed

    //TextFile Employee Code khi bấm phím Enter
    private void tfEmpCodeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfEmpCodeKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER || evt.getKeyCode() == KeyEvent.VK_TAB) {
            tfPass.requestFocus();
        }
    }//GEN-LAST:event_tfEmpCodeKeyPressed
    private void loginAction() {
        EmpCode = tfEmpCode.getText().trim();
        if (isEmpty(EmpCode)) { // Check xem Employee Code có rỗng không
            return;
        }
        String Pass = tfPass.getText().toString();
        if (isEmpty(Pass)) { // Check xem Password có rỗng không
            return;
        }
        // gọi phương thức login từ Đối tượng Login ở Package DAO
        if (login.login(EmpCode, Pass, tfEmpCode, tfPass, lbShowMessage)) {   // Nếu EMP_CODE và Password hợp lệ
            System.out.println("OK");
            frm_STAFF_A02 frm2 = new frm_STAFF_A02(EmpCode, connect);
            frm2.setVisible(true);
            this.dispose();
        }

    }

    // Nút Login khi nhấn phím Enter hoặc SpaceBar
    private void btnLoginKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnLoginKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER || evt.getKeyCode() == KeyEvent.VK_SPACE) {
            loginAction();
        }

    }//GEN-LAST:event_btnLoginKeyPressed

    //Textfield Password khi nhấn phím Enter
    private void tfPassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPassKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            loginAction();
        }

    }//GEN-LAST:event_tfPassKeyPressed

    private void tfEmpCodeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfEmpCodeFocusLost
        // TODO add your handling code here:
        if (tfEmpCode.getText().isEmpty()) {
            lbEmpCodeTest.setVisible(true);
            lbShowMessage.setVisible(true);
            lbShowMessage.setText("Employee Code cannot be empty");
            tfEmpCode.requestFocus();

        } else {
            lbEmpCodeTest.setVisible(false);
            lbShowMessage.setVisible(false);
        }
    }//GEN-LAST:event_tfEmpCodeFocusLost

    private void tfPassFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPassFocusLost
        // TODO add your handling code here:
        if (tfPass.getText().isEmpty()) {
            lbPasswordTest.setVisible(true);
            lbShowMessage.setVisible(true);
            lbShowMessage.setText("Password cannot be empty");
            tfPass.requestFocus();
        } else {
            lbPasswordTest.setVisible(false);
            lbShowMessage.setVisible(false);
        }
    }//GEN-LAST:event_tfPassFocusLost
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frm_STAFF_A01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frm_STAFF_A01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frm_STAFF_A01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frm_STAFF_A01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frm_STAFF_A01().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lbEmpCodeTest;
    private javax.swing.JLabel lbPasswordTest;
    private javax.swing.JLabel lbShowMessage;
    private javax.swing.JTextField tfEmpCode;
    private javax.swing.JPasswordField tfPass;
    // End of variables declaration//GEN-END:variables

    private boolean isEmpty(String value) {
        if (value.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}
